﻿using MvvmFoundation.Wpf;
using ServiceOperater.Behaviors;
using ServiceOperater.Helpers;
using ServiceOperater.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;

namespace ServiceOperater.ViewModels
{
    //  public sealed class FileTreeNodeViewModel : ObservableObject
    public sealed class ServiceTreeNodeViewModel : ObservableObject, IFeedback
    {
        public ServiceTreeNodeViewModel(String id, FileTreeNodeViewModel locationFile)
        {
            this.Id = id;
            this.OriginId = this.Id;
            this.ActionChildren = new ObservableCollection<ActionTreeNodeViewModel>();
            this.FoundFileInfoList = new ObservableCollection<FoundFileInfo>();
            this.LocationFile = locationFile;

            instantiateCommand();
        }

        private void instantiateCommand()
        {
            CopyFileNameCommand = new RelayCommand<String>((fileFullPath) =>
            {
                var fileNameHasExtension = Path.GetFileName(fileFullPath);
                // 第二个参数设置为false，或者不设置，将在程序运行结束后不保留剪切板内容。
                Clipboard.SetDataObject(fileNameHasExtension, true);
            }, (selectedFilePath) =>
            {
                return true;
            });

            AddActionCommand = new RelayCommand<ServiceTreeNodeViewModel>((selectedService) =>
            {
                // TODO 确保不重复
                var action = new ActionTreeNodeViewModel("~~~~", selectedService)
                {
                    IsSelected = true,
                    IsVirtual = true
                };
                this.ActionChildren.Add(action);
            }, (selectedService) =>
            {
                return true;
            });
        }

        #region Props
        // 我们拿id作为唯一性标识, 而该id是可能被用户更改的
        public String OriginId { get; set; }

        private String id = String.Empty;
        public String Id
        {
            get
            {
                return id;
            }
            set
            {
                if (id == value)
                {
                    return;
                }

                id = value;

                RaisePropertyChanged("Id");
            }
        }

        // ----
        // 所处的文件位置
        public FileTreeNodeViewModel LocationFile { get; private set; }

        private bool isModified = false;
        public bool IsModified
        {
            get
            {
                return isModified;
            }
            set
            {
                if (isModified == value)
                {
                    return;
                }

                isModified = value;

                RaisePropertyChanged("IsModified");
            }
        }


        //// 被引用的次数
        //private int num = 0;
        //public int Num
        //{
        //    get
        //    {
        //        return num;
        //    }
        //    set
        //    {
        //        if (num == value)
        //        {
        //            return;
        //        }

        //        num = value;

        //        RaisePropertyChanged("Num");
        //    }
        //}

        // 旗下的内容是否被修改
        private bool isHighight = false;
        public bool IsHighight
        {
            get
            {
                return isHighight;
            }
            set
            {
                if (isHighight == value)
                {
                    return;
                }

                isHighight = value;

                RaisePropertyChanged("IsHighight");
            }
        }

        private bool isVisible = true;
        public bool IsVisible
        {
            get
            {
                return isVisible;
            }
            set
            {
                if (isVisible == value)
                {
                    return;
                }

                isVisible = value;

                RaisePropertyChanged("IsVisible");
            }
        }


        // 作为tree的节点数据源时, 是否处于编辑状态
        private bool isInEditMode = false;
        public bool IsInEditMode
        {
            get
            {
                return isInEditMode;
            }
            set
            {
                if (isInEditMode == value)
                {
                    return;
                }

                isInEditMode = value;

                RaisePropertyChanged("IsInEditMode");
            }
        }

        // 指示该service是否在文件中有相应的内容
        private bool isVirtual = false;
        public bool IsVirtual
        {
            get
            {
                return isVirtual;
            }
            set
            {
                if (isVirtual == value)
                {
                    return;
                }

                isVirtual = value;

                RaisePropertyChanged("IsVirtual");
            }
        }

        // 引用这个service的文件相关位置信息
        public ObservableCollection<FoundFileInfo> FoundFileInfoList { get; private set; }

        private bool isExpanded = true;
        public bool IsExpanded
        {
            get
            {
                return isExpanded;
            }
            set
            {
                if (isExpanded == value)
                {
                    return;
                }

                isExpanded = value;

                RaisePropertyChanged("IsExpanded");
            }
        }

        private bool isSelected = false;
        public bool IsSelected
        {
            get
            {
                return isSelected;
            }
            set
            {
                if (isSelected == value)
                {
                    return;
                }

                isSelected = value;

                RaisePropertyChanged("IsSelected");
            }
        }

        public ObservableCollection<ActionTreeNodeViewModel> ActionChildren { get; private set; }
        #endregion

        #region Commands

        public RelayCommand<String> CopyFileNameCommand { get; private set; }

        public RelayCommand<ServiceTreeNodeViewModel> AddActionCommand { get; private set; }
        #endregion

        #region 枚举数据源

        #endregion

        void IFeedback.Feedback(Dictionary<string, Tuple<object, object>> modifyVals, Object dataContext)
        {
            if (dataContext != this)
            {
                return;
            }

            // NO OP
        }

        void IFeedback.RetrievalChange(Dictionary<string, Tuple<object, object>> modifyVals, Object dataContext)
        {
            if (dataContext != this)
            {
                return;
            }


            // 我们需要原始name去更新文件
            var idStr = ReflectHelper.GetMemberName((ServiceTreeNodeViewModel s) => s.Id);
            if (!modifyVals.ContainsKey(idStr))
            {
                return;
            }

            var originValOfId = modifyVals[idStr].Item1;

            this.OriginId = originValOfId.ToString();
        }
    }


}
